package de.riceman.simpleGo.view.dialogs;

import org.springframework.richclient.application.Application;
import org.springframework.richclient.dialog.TabbedDialogPage;
import org.springframework.richclient.dialog.TitledPageApplicationDialog;

import de.riceman.simpleGo.events.SimpleGoLifecycleApplicationEvent;
import de.riceman.simpleGo.main.BitMatrix;
import de.riceman.simpleGo.main.GoGame;
import de.riceman.simpleGo.main.HumanMoveSetup;
import de.riceman.simpleGo.main.NewGameSetup;
import de.riceman.simpleGo.view.forms.HumanMoveForm;
import de.riceman.simpleGo.view.forms.NewGameForm;
 
public class HumanMoveDialog extends TitledPageApplicationDialog {
 
	TabbedDialogPage compositePage;
	HumanMoveForm humanMoveForm;
	
	public HumanMoveDialog(TabbedDialogPage compositePage, HumanMoveForm humanMoveForm) {
		super(compositePage);
		this.compositePage = compositePage;
		this.humanMoveForm = humanMoveForm;
	}
	
	protected void onAboutToShow() {
		String title =	getMessage("humanMoveProperties.edit.title");
		setTitle(title);
		
        setEnabled(compositePage.isPageComplete());
    }

	protected boolean onFinish() {
		humanMoveForm.getFormModel().commit();
        
        HumanMoveSetup humanMoveSetup = (HumanMoveSetup) humanMoveForm.getFormObject();
        
        //convert HumanMoveSetup object to BitMatrix
        GoGame goGame = (GoGame) Application.instance().getApplicationContext().getBean("GoGame");
        
        BitMatrix move = new BitMatrix(1, goGame.getConfiguration().getSize() * goGame.getConfiguration().getSize());
        
        if (!humanMoveSetup.isPass()) {

            move.set(
            		(Integer.parseInt(humanMoveSetup.getxPos()) * goGame.getConfiguration().getSize()) 
            		+ Integer.parseInt(humanMoveSetup.getyPos()));
        }
        
        //the event is thrown here instead of the player's generateMove() method!
        getApplicationContext().publishEvent(
        		new SimpleGoLifecycleApplicationEvent(SimpleGoLifecycleApplicationEvent.MOVE_GENERATED, move));
        
        return true;
    }
	
	@Override
	protected void onCancel() {
		super.onCancel();
	}
	
     
}